use strict;

if( scalar(@ARGV) != 1 )
{
	print "USAGE: $0 <pattern_coff_file_to_parse>\n";
}

my $file_to_parse = @ARGV[0];

open( FILE, "<$file_to_parse" ) or die "can't open $file_to_parse: $!";

my $line;
my $inner_string;
my $outer_string;
my $cmd;

while( $line = <FILE> )
{
	chomp $line;

	if ( !($line =~ "Best Coff For Pattern:") ) 
	{
		next;
	}

	my ( $junk1, $junk2, $junk3, $junk5, $junk6 ) = split / /, $line;
	$line = $junk6;

	my ($pattern, $coff) = split /;/, $line;

	my @pattern_array = split /,/, $pattern;
	my $i;
	my $inner;
	my $outer;
	foreach $i ( 0..13)
	{
		$outer .= $pattern_array[$i];
	}

	foreach $i ( 14..19)
	{
		$inner .= $pattern_array[$i];
	}

	$pattern = "$inner $outer";


	$cmd = "draw_pattern.pl $pattern";
	print "PATTERN: $pattern\n";

	open( OUTPUT, "$cmd|" );

	while ( $line = <OUTPUT> )
	{
		print $line;
	}
	print "\n";

	close OUTPUT;

	@pattern_array = split /,/, $coff;
	my $i;
	$outer = $pattern_array[0];
	foreach $i ( 1..13)
	{
		$outer .= ",";
		$outer .= $pattern_array[$i];
	}

	$inner = $pattern_array[14];
	foreach $i ( 15..19)
	{
		$inner .= ",";
		$inner .= $pattern_array[$i];
	}

	$pattern = "$inner $outer";

	$cmd = "draw_coff_pattern.pl $pattern";
	print "COFF:\n";
	open( OUTPUT, "$cmd|" );

	while ( $line = <OUTPUT> )
	{
		print $line;
	}
	print "\n";

	close OUTPUT;



	my $sum = 0;
	for $i ( 0..19 )
	{
		$sum += $pattern_array[$i];
	}

	for $i ( 0..19 )
	{
		$pattern_array[$i] = int ($pattern_array[$i] * 100 / $sum );
		if ( $pattern_array[$i] <= 3 && $pattern_array[$i] >= -3 )
		{
			$pattern_array[$i] = 0;
		}
	}


	$outer = $pattern_array[0];
	foreach $i ( 1..13)
	{
		$outer .= ",";
		$outer .= $pattern_array[$i];
	}

	$inner = $pattern_array[14];
	foreach $i ( 15..19)
	{
		$inner .= ",";
		$inner .= $pattern_array[$i];
	}

	$pattern = "$inner $outer";

	$cmd = "draw_coff_pattern.pl $pattern";
	print "COFF in percent:\n";
	open( OUTPUT, "$cmd|" );

	while ( $line = <OUTPUT> )
	{
		print $line;
	}
	print "\n";

	close OUTPUT;



}
